package j2bs;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Faraj
 */
public class J2BServer extends Thread
{
    boolean isWorking;
    ServerSocket ControlServer;
    ServerSocket DataServer;
    private static J2BServer j2bserver = null;
    public static File storage;
    public String path;

    public static void startService(int cport,int dport, String path) throws IOException
    {
        if(j2bserver != null)
        {
            j2bserver.ControlServer.close();
        }
        j2bserver = new J2BServer(cport,dport);

        j2bserver.path = path;
        if(j2bserver.InitStorage())
            j2bserver.start();

    }

    public static void stopService() throws IOException
    {
        if(j2bserver == null)
            return;
        j2bserver.stop();
        j2bserver.ControlServer.close();
        j2bserver.DataServer.close();
        j2bserver = null;
    }


    private J2BServer(int controlPort, int dataPort)
    {
        try {
            ControlServer = new ServerSocket(controlPort);
            DataServer = new ServerSocket(dataPort);
            isWorking = true;
        } catch (IOException ex) {
            isWorking = false;
        }
    }

    @Override
    public void run()
    {
        if(!isWorking)
        {
            return;
        }

        while(true)
        {
            try
            {
                Socket cSock = ControlServer.accept();
                Socket dSock = DataServer.accept();
                if(isAllowed(cSock.getInetAddress().getHostAddress()))
                    new J2BService(cSock,dSock);
            }
            catch (Exception ex)
            {
                continue;
            }

        }
    }

    private boolean isAllowed(String IP)
    {
        BufferedReader reader = null;
        try
        {
            FileReader fileIn = new FileReader(path+"\\WEB-INF\\hosts.allowed");
            reader = new BufferedReader(fileIn);
            String value = "";
            while((value = reader.readLine()) != null)
            {
                if(value.equalsIgnoreCase(IP))
                {
                    try {
                        reader.close();
                    } catch (IOException ex) {
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }
        catch(Exception e)
        {

            return false;
        }
        finally
        {
            try {
                reader.close();
            } catch (IOException ex) {
                return false;
            }
        }
    }

     public boolean InitStorage()
    {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path+"\\WEB-INF\\storage.path"));
            storage = new File(reader.readLine());
        } catch (Exception ex) {
            return false;
        }

        if(!storage.exists())
        {
            storage.mkdir();
        }
        else if(!storage.canExecute()||!storage.canRead()||storage.isFile())
        {
            return false;
        }
        return true;
    }
}
